package com.hntxk.cqzd.sysconfig.sms.service.impl;


import com.hntxk.core.framework.model.ResultModel;
import com.hntxk.core.framework.service.impl.BaseServiceImpl;
import com.hntxk.cqzd.basicinfo.basepatient.entity.BasePatient;
import com.hntxk.cqzd.sysconfig.sms.entity.SmsTmpl;
import com.hntxk.cqzd.sysconfig.sms.service.SmsTmplManageService;
import com.hntxk.sp.log.model.ActionLog;
import com.hntxk.sp.log.model.ActionType;
import com.hntxk.sp.util.LogRecordUtil;
import com.hntxk.sp.util.LoginUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author <a href="huangbiquan@hotmail.com">Huang.Biquan</a>
 * @Description: 基本信息管理接口实现类 <br>
 * @Project: SP <br>
 * @date 2019/6/3 10:01 <br>
 */
@Service("smsTmplService")
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class SmsTmplManageServiceImpl extends BaseServiceImpl<SmsTmpl> implements SmsTmplManageService {


    public static String MAPPER_NAMESPACE = "com.hntxk.cqzd.sysconfig.sms.mapper.SmsTmplMapper.";

    public SmsTmplManageServiceImpl() {
        super(MAPPER_NAMESPACE);
    }

    //新增短信模板
    @Override
    public ResultModel<SmsTmpl> insert(SmsTmpl smsTmpl) {
        getPrimaryKey(smsTmpl);
        smsTmpl.setCreateTime(new Date());
        smsTmpl.setCreateUserId(LoginUserUtil.getLoginOperatorIdLong());
        smsTmpl.setCreateUserName(LoginUserUtil.getLoginUserName());
        insert(MAPPER_NAMESPACE+"insert",smsTmpl);
        //记录日志
        LogRecordUtil.addLog(new ActionLog(SmsTmpl.class, smsTmpl.getTmplId(), "12",
                ActionType.ADD, "新增短信模板：" + smsTmpl.getTmplName() + "[ " + smsTmpl.getTmplContent() + " ]"));
        return ResultModel.of(smsTmpl);
    }

    //修改短信模板
    @Override
    public ResultModel<SmsTmpl> update(SmsTmpl smsTmpl) {
        getPrimaryKey(smsTmpl);
        smsTmpl.setUpdateTime(smsTmpl.getCreateTime());
        smsTmpl.setUpdateUserId(smsTmpl.getCreateUserId());
        smsTmpl.setUpdateUserName(smsTmpl.getCreateUserName());
        super.update(MAPPER_NAMESPACE + "update", smsTmpl);
        //记录日志
        LogRecordUtil.addLog(new ActionLog(BasePatient.class, smsTmpl.getTmplId(), "12",
                ActionType.ADD, "修改了短信模板：" + smsTmpl.getTmplName() + "[ " + smsTmpl.getTmplContent()  + " ]"));
        return ResultModel.of(smsTmpl);
    }

    //删除短信模板
    @Override
    public void delete(String ids, boolean isCalled) {
        List<String> list = Arrays.asList(ids.split(","));
        List<SmsTmpl> smsTmpls=new ArrayList<>();
        for (String id : list) {
            smsTmpls.add(get(Long.parseLong(id)));
            delete(MAPPER_NAMESPACE+"delete",id);
        }
        for(SmsTmpl smsTmpl:smsTmpls)
            LogRecordUtil.addLog(new ActionLog(BasePatient.class, smsTmpl.getTmplId() + "", "12",
                    ActionType.DELETE, "删除短信模板：" + smsTmpl.getTmplName() + "[ " + smsTmpl.getTmplContent() + " ]"));
    }
}
